<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_advertising\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{

    public function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    public function index()
    {
        $param = $this->request->param();
        if(!empty($param['pid'])){
            $where['position_id'] = ['eq',$param['pid']];
            $pid = $param['pid'];
        }else{
            $where = '';
            $pid = '';
        }
        $lists = Db::name('ad')->where($where)->order('id desc')->paginate(10);
        $page = $lists->render();

        $this->assign('page', $page);
        $this->assign('lists', $lists);

        $lists1 = Db::name('ad_position')->order('id desc')->select();
        $this->assign('posit', $lists1);
        $this->assign('pid', $pid);

        return $this->fetch('/index');
    }

    public function add()
    {
        $lists = Db::name('ad_position')->order('id desc')->select();
        $this->assign('lists', $lists);
        return $this->fetch('/add');
    }

    /**
     * @adminLog(
     *     'public/plugin/qbt_advertising/admin_index/addpost|添加广告:{ad_name}',
     * )
     */
    public function addpost(){
        $param = $this->request->param();
        if($param['position_id']==-1){
            $this->error('请选择广告位置');
        }
        $lists = Db::name('ad_position')->where(['id'=>$param['position_id']])->find();
        
        if($lists['media_type']==0){
            if(!$param['ad_img']){
                $this->error('请上传图片！');
            } 
        }

        $data = [
            'position_id'=>$param['position_id'],//广告位ID
            'ad_name'=>$param['ad_name'],//广告名称            
            'ad_link'=>$param['ad_link'],//广告链接
            'ad_code'=>$param['ad_code'],//自定义代码
            'ad_text'=>$param['ad_text'],//广告内容
            'start_time'=>strtotime($param['start_time']),//开始时间
            'end_time'=>strtotime($param['end_time']),//结束时间
            'ad_img'=>$param['ad_img'],//缩略图
            'enabled'=>$param['enabled']//是否开启
        ];
        
        Db::name('ad')->insert($data);

        $this->success('添加成功!', cmf_plugin_url('QbtAdvertising://AdminIndex/index'));
    }    

    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');

        $lists = Db::name('ad')->where(['id'=>$id])->find();

        $this->assign('post', $lists);

        $lists1 = Db::name('ad_position')->order('id desc')->select();
        $this->assign('lists', $lists1);

        return $this->fetch('/edit');
    }

    /**
     * @adminLog(
     *     'plugin/qbt_advertising/admin_index/editpost|编辑广告:{ad_name}',
     * )
     */
    public function editpost(){
        $param = $this->request->param();
        if($param['position_id']==-1){
            $this->error('请选择广告位置');
        }
        $lists = Db::name('ad_position')->where(['id'=>$param['position_id']])->find();
        
        if($lists['media_type']==0){
            if(!$param['ad_img']){
                $this->error('请上传图片！');
            } 
        }

        $hook['position_id'] = $param['position_id'];
        $hook['ad_name'] = $param['ad_name'];        
        $hook['ad_link'] = $param['ad_link'];
        $hook['ad_code'] = $param['ad_code'];
        $hook['ad_text'] = $param['ad_text'];
        $hook['start_time'] = strtotime($param['start_time']);
        $hook['end_time'] = strtotime($param['end_time']);
        $hook['ad_img'] = $param['ad_img'];
        $hook['enabled'] = $param['enabled'];
        
        Db::name('ad')->where(['id' => $param['id']])->update($hook);

        $this->success('编辑成功!', cmf_plugin_url('QbtAdvertising://AdminIndex/index'));
    }  


    public function ads_index()
    {
        $lists = Db::name('ad_position')->order('id desc')->paginate(10);
        $page = $lists->render();

        $this->assign('page', $page);
        $this->assign('lists', $lists);
        return $this->fetch('/ad_index');
    }

    public function ad_add()
    {
        return $this->fetch('/ad_add');
    }

    /**
     * @adminLog(
     *     'plugin/qbt_advertising/admin_index/adaddpost|添加广告位:{position_name}',
     * )
     */
    public function adaddpost(){
        $param = $this->request->param();
        $data = [
            'position_name'=>$param['position_name'],//广告位名称
            'ad_logo'=>$param['ad_logo'],//广告标识
            'media_type'=>$param['media_type'],//媒介类型
            // 'ad_width'=>$param['ad_width'],//广告位宽度
            // 'ad_height'=>$param['ad_height'],//广告位高度
            'position_desc'=>$param['position_desc']//广告位描述
        ];
        
        Db::name('ad_position')->insert($data);

        $this->success('添加成功!', cmf_plugin_url('QbtAdvertising://AdminIndex/ads_index'));
    }

    public function ad_edit()
    {
        $id = $this->request->param('id', 0, 'intval');

        $lists = Db::name('ad_position')->where(['id'=>$id])->find();

        $this->assign('post', $lists);

        return $this->fetch('/ad_edit');
    }

    /**
     * @adminLog(
     *     'plugin/qbt_advertising/admin_index/adeditpost|编辑广告位:{position_name}',
     * )
     */
    public function adeditpost(){
        $param = $this->request->param();

        $hook['position_name'] = $param['position_name'];
        $hook['ad_logo'] = $param['ad_logo'];
        $hook['media_type'] = $param['media_type'];
        $hook['position_desc'] = $param['position_desc'];
        
        Db::name('ad_position')->where(['id' => $param['id']])->update($hook);

        $this->success('编辑成功!', cmf_plugin_url('QbtAdvertising://AdminIndex/ads_index'));
    }

    /**
     * @adminLog(
     *     'plugin/qbt_advertising/admin_index/delete|删除广告:{id}',
     * )
     */
    public function delete(){
        Db::name('ad')->where(['id'=>$this->request->param('id', 0, 'intval')])->delete();
        $this->success('删除成功');
    }

    /**
     * @adminLog(
     *     'plugin/qbt_advertising/admin_index/ad_delete|删除广告位置:{id}',
     * )
     */
    public function ad_delete(){
        $param = $this->request->param();
        $count = Db::name('ad')->where('position_id', $param['id'])->count();

        if ($count > 0) {
            $this->error('此广告位有广告无法删除!');
        }


        Db::name('ad_position')->where(['id'=>$param['id']])->delete();
        $this->success('删除成功');
    }

    // public function deletes(){
    //     $param = $this->request->param();
    //     foreach($param['ids'] as $k=>$v){
    //         Db::name('advertising')->where('id',$v)->delete();
    //     }
    //     //Db::name('guestbook')->where(['id'=>$this->request->param('id', 0, 'intval')])->delete();
    //     $this->success('删除成功');
    // }

    public function lxajax(){

        $id = $_REQUEST['id'];

        $lists = Db::name('ad_position')->where(['id'=>$id])->find();

        echo $lists['media_type'];
    }

}
